package com.duoketech.service.web.article;
import com.duoketech.config.Constant;
import com.duoketech.service.AbstractService;
import com.duoketech.model.article.Article;
import com.duoketech.scrawler.WxActivityBean;
import com.duoketech.scrawler.WxJsoup;
import com.duoketech.exception.ValidateException;
import com.duoketech.utils.search.EqualCondition;
import com.duoketech.utils.search.LikeCondition;
import com.duoketech.utils.search.QueryConstructor;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import org.apache.commons.lang3.StringUtils;

public class ArticleService extends AbstractService{
    public static ArticleService ME = new ArticleService();

    @Override
    public void validatorUpdate(Model<?> model, Kv kv) throws ValidateException {
        validatorAdd(model,kv);
    }

    @Override
    public void validatorAdd(Model<?> model, Kv kv) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(model.getStr(Article.TITLE),"标题信息项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Article.TITLE),"标题信息项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Article.COVER),"封面图片项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Article.COVER),"封面图片项字符必须为1-150个字符",1,150));
        validator.addRule(new StringMustBeRequired(model.getStr(Article.DESC),"描述信息项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Article.DESC),"描述信息项字符必须为1-150个字符",1,150));
//        validator.addRule(new StringMustBeRequired(model.getStr(Article.STATUS),"状态项为必填信息"));
//        validator.addRule(new StringLengthBetween(model.getStr(Article.STATUS),"状态项字符必须为1-50个字符",1,50));
        validator.excute();

    }

    @Override
    public void beforAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public String tableName() {
        return Article.dao.tableName();
    }

    @Override
    public void builderSQLExpected(Model<?> model, QueryConstructor queryConstruct, Kv kv) {

        queryConstruct.and(new EqualCondition(Article.STATUS,Article.STATUS_NORMAL));
        queryConstruct.and(new EqualCondition(Article.MEMBER_ID,kv.getInt(Constant.MEMBER_ID_KEY)));
        String value = model.getStr(Article.TITLE);
        if(StringUtils.isNotEmpty(value)){
            queryConstruct.and(new LikeCondition(Article.TITLE,"%"+value+"%"));
        }
    }
    
    
    public WxActivityBean doWxSync(String url){
    	return WxJsoup.readContentFromWeiXin(url);
    }
}